/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once

#include <math.h>
#include <memory.h>
#include <stdlib.h>
#include "../../types.h"

class	EVector;
class	EPoint;
class	ETexCoord;
class	EMatrix;
class	EQuaternion;

#include "point.h"
#include "vector.h"
#include "quaternion.h"
#include "color.h"
#include "texcoord.h"
#include "matrix.h"

#include "point_imp.h"
#include "vector_imp.h"
#include "texcoord_imp.h"
#include "color_imp.h"

#include "transform.h"

#include "mersenne.h"

//	old
#include "_old_bbox.h"
#include "_old_plane.h"
#include "_old_sphere.h"
#include "_old_frustum.h"

#include "camera.h"

/*-----------------------------------------------------------------------------
	MATH :
-----------------------------------------------------------------------------*/

class EMath {
	public:
		static float		Rad		( float a )	{ return a/57.295779513260928129089516840402f; }
		static float		Deg		( float a )	{ return a*57.295779513260928129089516840402f; }
		static float		Sqr		( float a ) { return a*a; }
		static float		Sign	( float a ) { return (a >= 0) ? 1.0f : -1.0f; }
		static float		Frac	( float a );
		static float		Randf	( float a=0, float b=1 );
		static void			SinCos	( float a, float &s, float &c ) { s = sin(a); c = cos(a); }
		
		static float		Dot		( const EVector &a, const EVector &b ) { return a.Dot( b ); }
		static EVector		Cross	( const EVector &a, const EVector &b ) { return a.Cross( b ); }
		static bool			Compare	( const EVector	  &a, const EVector	  &b, float eps ) { return a.IsEqual(b, eps); }
		static bool			Compare	( const EPoint	  &a, const EPoint	  &b, float eps ) { return a.IsEqual(b, eps); }
		static bool			Compare	( const EColor	  &a, const EColor	  &b, float eps ) { return a.IsEqual(b, eps); }
		static bool			Compare	( const ETexCoord &a, const ETexCoord &b, float eps ) { return a.IsEqual(b, eps); }
		static EVector		Lerp	( const EVector   &a, const EVector   &b, float factor ) { return a.Lerp(b, factor); }
		static EPoint		Lerp	( const EPoint	  &a, const EPoint	  &b, float factor ) { return a.LerpTo(b, factor); }
		static EColor		Lerp	( const EColor	  &a, const EColor	  &b, float factor ) { return a.LerpTo(b, factor); }
		static EQuaternion	SLerp	( const EQuaternion &a, const EQuaternion &b, float factor ) { return a.SLerp(b, factor); }
	};
	
	
inline EVector	operator - ( const EPoint &a, const EPoint &b ) { return EVector(b, a); }
	
/*-----------------------------------------------------------------------------
	MATH :
-----------------------------------------------------------------------------*/

//
//	EMath::Randf
//
inline float EMath::Randf( float a/* =0 */, float b/* =1 */ )
{
	float fr = rand() / (float)RAND_MAX;
	return a + fr * (b-a);
}


inline float EMath::Frac( float a )
{
	float dummy;
	return modf(a, &dummy);
}	
	

template<class Type> Type clamp(Type a, Type low, Type high)
{
	if (a > high) a = high;
	if (a < low) a = low;
	return a;
}


#undef PI
const float PI	=	3.14159265358f;

const float MAX_FLOAT_VALUE	=	3.40E38f;
const float MIN_FLOAT_VALUE	=	1.18E-38f;
const float	FLOAT_EPSILON	=	1.192092896e-07f;	//	smallest positive number such that 1.0+FLT_EPSILON != 1.0


template<class Type> void Swap(Type &a, Type &b)
{
	Type t = a;
	a = b;
	b = t;
}

bool Math_RayTriangleIntersection( EPoint &result, float &fraction, const EPoint &origin, const EVector &dir, 
												const EPoint &v0,  const EPoint &v1,  const EPoint &v2 );







